﻿using Publicuse.DBCore.Oracle;
using Publicuse.Interface;
using Publicuse.Entity;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service
{
    /// <summary>
    /// 统一业务服务
    /// </summary>
    public class MyOracleService : IMyOracleService
    {
        private int dbIns = -1;

        public MyOracleService()
        {
            this.dbIns = AppConfigUtil.ReadAppSettings("dbOracleType").GetIntValue();
        }

        /// <summary>
        /// 业务新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int AddInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Add, out msg);
        }

        /// <summary>
        /// 批量业务新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<int> AddInfo<T>(List<T> t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Add, out msg);
        }

        /// <summary>
        /// 业务删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int DeleteInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Delete, out msg);
        }

        public int DeleteInfo<T>(T t, out string msg, string sql)
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Delete, out msg, sql);
        }

        /// <summary>
        /// 业务修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int UpdateInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Update, out msg);
        }

        /// <summary>
        /// 批量业务修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<int> UpdateInfo<T>(List<T> t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Update, out msg);
        }

        /// <summary>
        /// 业务查询列表信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlParameters"></param>
        /// <param name="isproc"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        public void GetInfoList<T>(string sql, object[] sqlParameters, bool isproc, out string msg, out List<T> list) where T : class, new()
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetList<T>(sql, sqlParameters, isproc, out msg, out list);
        }

        /// <summary>
        /// 分页查询列表信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbIns"></param>
        /// <param name="storedprocedure"></param>
        /// <param name="tablename"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlWhere"></param>
        /// <param name="sortFile"></param>
        /// <param name="srotType"></param>
        /// <param name="count"></param>
        /// <param name="exception"></param>
        /// <param name="list"></param>
        public void GetInfoList<T>(string storedprocedure, string tablename, string queryFile, int pageIndex, int pageSize, string sqlWhere, string sortFile, string srotType, out long count, out string exception, out List<T> list) where T : class, new()
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetList<T>(storedprocedure, tablename, queryFile, pageIndex, pageSize, sqlWhere, sortFile, srotType, out count, out exception, out list);
        }

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        /// <param name="totalCount"></param>
        public void GetList<T>(CommonQuery query, out string msg, out List<T> list, out long totalCount) where T : class, new()
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetList<T>(query.storedProcedureName, query.tableName, query.files, query.pageIndex, query.pageSize, query.sqlWhere, query.sortFile, query.sortType, out totalCount, out msg, out list);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        public void GetList<T>(CommonQuery query, out string msg, out List<T> list) where T : class, new()
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetList<T>(query.sql, null, false, out msg, out list);
        }

        /// <summary>
        /// 业务查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="sqlParameters"></param>
        /// <param name="msg"></param>
        /// <param name="t"></param>
        public void QueryInfoByID<T>(int id, object[] sqlParameters, out string msg, out T t)
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetEntityById<T>(id, out msg, out t);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        public void QueryInfoByID<T>(int id, out string msg, out T data)
        {
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetEntityById<T>(id, out msg, out data);
        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="param"></param>
        /// <param name="isproc"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sqlstr, object param, bool isproc, out string exception)
        {
            DataSet dataSet = new DataSet();
            DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetDataSet(sqlstr, param, isproc, out exception, out dataSet);
            return dataSet;
        }

        /// <summary>
        /// 获取数据表
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="param"></param>
        /// <param name="isproc"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public bool GetDataTable(string sqlstr, object param, bool isproc, out string exception, out DataTable dt)
        {
            return DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).GetDataTable(sqlstr, param, isproc, out exception, out dt);
        }



        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="msg"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public bool ExecuteSql(string sql, out string msg)
        {
            msg = null;
            try
            {
                DBHelperUtil<MyOracleInstance>.GetInstance(dbIns).ExecuteSql(sql, out msg, (db) =>
                {
                    db.ExecuteNonQuery();
                    return true;
                });
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }



        public int Update<T>(string json, int id, out string msg) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public void QueryInfoByID<T>(long id, object[] sqlParameters, out string msg, out T t) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public void QueryInfoByID<T>(long id, out string msg, out T data) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public bool GetDataSet(string sql, out string exception, out DataSet dataSet)
        {
            throw new NotImplementedException();
        }

        public DataTable GetDataTable(string sqlstr, List<Tuple<string, string, ExcuteType>> tuples, out string exception)
        {
            throw new NotImplementedException();
        }

        public bool GetList<T>(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string exception, out List<T> list) where T : new()
        {
            throw new NotImplementedException();
        }

        public void QueryInfoBySql<T>(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string msg, out T t) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public bool ExecuteSql(string sql, Dictionary<string, string> pairs, out string msg, ExcuteType type = ExcuteType.Equal)
        {
            throw new NotImplementedException();
        }

        public bool ExecuteSql(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string msg)
        {
            throw new NotImplementedException();
        }

        public bool ExecuteSqls(List<Tuple<string, Dictionary<string, string>>> pairs, out string msg)
        {
            throw new NotImplementedException();
        }
    }
}
